//
//  DepartmentsViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14/10/31.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "DepartmentsViewController.h"
#import "ContactTabelViewController.h"
#import "Department.h"
#import "Contact.h"

@interface DepartmentsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableViewCell *tableView;
@property (strong,nonatomic)NSArray *departments;

@end

@implementation DepartmentsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"部门";
    [self initData];
}


-(void)initData{
    _departments = [Department getDepartments];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48.0f;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  _departments.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    static NSString *identifierCell = @"IdentifierCell";
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifierCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierCell];
        cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    }
    Department *department = _departments[indexPath.row];
    cell.textLabel.text = department.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ContactTabelViewController *contactTabelViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactTabelViewController"];
    Department *department = _departments[indexPath.row];
    [contactTabelViewController initContacts:[Contact departmentContacts:department.ID] withContent:department.name];
    [self.navigationController pushViewController:contactTabelViewController animated:YES];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
